package com.dtagri.fjwlw.fragment;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import android.view.View;
import android.widget.Toast;

import com.dtagri.fjwlw.AppContext;
import com.dtagri.fjwlw.R;
import com.dtagri.fjwlw.api.FJwlwApi;
import com.dtagri.fjwlw.base.BaseSimpleFragment;
import com.dtagri.fjwlw.base.TGson;
import com.dtagri.fjwlw.bean.SmallArea;
import com.dtagri.fjwlw.bean.TjAvgHourData;
import com.dtagri.fjwlw.bean.TjEquipment;
import com.dtagri.fjwlw.ui.IndexContrastActivity;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.google.gson.reflect.TypeToken;

/**
 * 指标对比
 * 
 * @author wxjn
 * 
 */
public class IndexContrastFragment extends BaseSimpleFragment {

	public LineChart mChart;
	
	protected List<List<TjAvgHourData>> tjAvgHourDatass = new ArrayList<List<TjAvgHourData>>();

	List<String> xVals = new ArrayList<String>();
	List<List<Entry>> yValss = new ArrayList<List<Entry>>();

	public void requestData() {
		initData();
	}

	@Override
	protected void xOnSuccess(byte[] arg2) {
		Type type = new TypeToken<List<List<TjAvgHourData>>>() {
		}.getType();
		tjAvgHourDatass = TGson.gson.fromJson(new String(arg2), type);
		convertData2();
		setData();
	}

	/**
	 * xVals,yVals赋值
	 */
	private void convertData2() {
		int i = 0;
		xVals.clear();
		yValss.clear();
		for (List<TjAvgHourData> tjAvgHourDatas : tjAvgHourDatass) {
			List<Entry> yVals = new ArrayList<Entry>();
			for (TjAvgHourData tjAvgHourData : tjAvgHourDatas) {
				String time = tjAvgHourData.getTime();
				String substring = time.substring(time.indexOf("-") + 1,
						time.length());
				xVals.add(substring);
				yVals.add(new Entry(tjAvgHourData.getAvgValue(), i));
				i++;
			}
			yValss.add(yVals);
		}
	}

	private int[] mColors = new int[] { ColorTemplate.VORDIPLOM_COLORS[0],
			ColorTemplate.VORDIPLOM_COLORS[1],
			ColorTemplate.VORDIPLOM_COLORS[2] };

	/**
	 * 更新曲线数据
	 */
	private void setData() {
		if(mChart.equals("[]")){
			System.out.println("数据异常");
			return;
		}
		//需要数据获取错误信息捕获
		mChart.resetTracking();
		
		ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>();
		int i = 0;
		for (List<Entry> yVals : yValss) {

			String smallName = getSmallAreaName(i);
			LineDataSet d = new LineDataSet(yVals, smallName);

			d.setLineWidth(2.5f);
			d.setCircleSize(4f);

			int color = mColors[yValss.size() % mColors.length];
			d.setColor(color);
			d.setCircleColor(color);
			dataSets.add(d);
			List<TjAvgHourData> list = tjAvgHourDatass.get(i);
			
				try {
					mChart.setDescription("单位:" + list.get(0).getUnit());
				} catch (Exception e) {
					// TODO Auto-generated catch block
//					System.out.println("异常");
					Toast.makeText(getActivity(), "暂无数据", 500).show();
				}
		

			i++;
		}

		LineData data = new LineData(xVals, dataSets);
		mChart.setData(data);
		mChart.invalidate();
	}

	/**
	 * 根据小区id查找小区名称
	 * 
	 * @param i
	 * @return
	 */
	public String getSmallAreaName(int i) {
		IndexContrastActivity indexContrastActivity = (IndexContrastActivity) baseSimpleActivity;
		List<Integer> smallAreaIDs = indexContrastActivity.smallAreaIDs();
		Integer integer = smallAreaIDs.get(i);
		List<SmallArea> smallAreas = indexContrastActivity.curEquipment()
				.getSmallAreas();
		String smallName = null;
		for (SmallArea smallArea : smallAreas) {
			if (smallArea.getAreaId() == integer) {
				smallName = smallArea.getSmallName();
			}
		}
		return smallName;
	}

	@Override
	public void initView(View view) {
		super.initView(view);
		mChart = (LineChart) view.findViewById(R.id.chart1);
		initLineChart();
	}

	/**
	 * 初始化曲线
	 */
	private void initLineChart() {

		// mChart.setOnChartValueSelectedListener(this);

		mChart.setDrawGridBackground(false);
		mChart.setDescription("");
//		 mChart.setStartAtZero(true);

		// enable value highlighting
		mChart.setHighlightEnabled(true);

		// enable touch gestures
		mChart.setTouchEnabled(true);

		// enable scaling and dragging
		mChart.setDragEnabled(true);
		mChart.setScaleEnabled(true);

		// if disabled, scaling can be done on x- and y-axis separately
		mChart.setPinchZoom(false);
		//曲线动态出现
//		Legend legend=mChart.getLegend();
//		legend.setTextColor(Color.GREEN);
//		legend.setForm(LegendForm.SQUARE);
//		legend.setFormSize(6f);
//		mChart.animateX(2500);
	}

	@Override
	protected void xInitData() {
		IndexContrastActivity indexContrastActivity = (IndexContrastActivity) baseSimpleActivity;
		List<Integer> smallAreaIDs = indexContrastActivity.smallAreaIDs();
		TjEquipment tjEquipment = indexContrastActivity.curEquipment();
		FJwlwApi.indexContrast(smallAreaIDs, tjEquipment.getEquipId(),
				AppContext.user.getCompanyId(), mHandler);
	}

	@Override
	protected int getLayoutId() {
		return R.layout.layout_index_contrast;
	}

	@Override
	protected String getCacheKey() {
		IndexContrastActivity indexContrastActivity = (IndexContrastActivity) baseSimpleActivity;
		TjEquipment tjEquipment = indexContrastActivity.curEquipment();
		StringBuffer stringBuffer = new StringBuffer("indexContrast");
		List<Integer> smallAreaIDs = indexContrastActivity.smallAreaIDs();
		for (Integer integer : smallAreaIDs) {
			stringBuffer.append(integer);
		}
		stringBuffer.append(tjEquipment.getEquipId()).append(
				AppContext.user.getCompanyId());
		return stringBuffer.toString();
	}
}
